package com.dangdang.recursion.test0805;

import com.dangdang.binarytree.Node;

import java.util.Stack;

/**
 * @Author Jiangnan Hou
 * @Date 2022/8/5
 */
public class BinaryTreeTraverse {

    public static void midleRecursion(Node root){
        if (root != null) {
            midleRecursion(root.getLeft());
            System.out.println(root.getValue());
            midleRecursion(root.getRight());
        }

    }

    public static void nonRecursion(Node root ){
        Stack<Node> stack = new Stack<>();
        while (root != null || !stack.empty()){
            while (root != null) {
                stack.push(root);
                root = root.getLeft();
            }
            root = stack.pop();
            System.out.println(root.getValue());
            root = root.getRight();
        }

    }

    public static void main(String[] args) {
        Node root = new Node();
        Node.generate(root);
        midleRecursion(root);
        System.out.println("分割线");
        nonRecursion(root);
    }



}
